-module(simpleone_serv).
-behaviour(gen_server).
-export([start_link/1]).
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         code_change/3,
         terminate/2]).
-record(state, {name, msgs}). % server state


%% API different from common server, no need of server definition
start_link(Name) ->
    gen_server:start(?MODULE, [Name], []).


%% Gen server
init(Name) ->
    {ok, #state{name=Name, msgs=[]}}.

handle_call(stop, From, State) ->
    NewState = #state{msgs=[{stop, From}|State#state.msgs]},
    Reply = ok,
    {stop, normal, Reply, NewState};

handle_call(Query, From, State) ->
    NewState = #state{msgs=[{Query, From}|State#state.msgs]},
    Reply = ok,
    {reply, Reply, NewState}.

handle_cast(stop, State) ->
    NewState = #state{msgs=[stop|State#state.msgs]},
    Reply = ok,
    {stop, Reply, NewState};

handle_cast(Msg, State) ->
    NewState = #state{msgs=[Msg|State#state.msgs]},
    {noreply, NewState}.

handle_info(Msg, State) ->
    io:format("Unknown msg: ~p~n", [Msg]),
    {noreply, State}.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

terminate(_Reason, _State) ->
    ok.